package com.xintool.window.Buttons;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.net.URI;

public class Button_info extends ButtonFactory {
    private static JFrame frame;
    private static final String UPDATE_URL = "https://wwtq.lanzouo.com/b007tjbpyh";

    @Override
    public JButton showButton() {
        JButton button = ButtonFactory.getButton("关于");
        button.addActionListener(e -> createMainWindow());
        return button;
    }

    public void createMainWindow() {
        if (frame != null && frame.isDisplayable()) {
            frame.toFront();
            return;
        }

        frame = new JFrame("关于");
        frame.setSize(300, 250);
        frame.setLayout(new BorderLayout(10, 10));
        frame.setResizable(false);
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

        // 主内容面板
        JPanel mainPanel = new JPanel(new BorderLayout(10, 10));
        mainPanel.setBorder(BorderFactory.createEmptyBorder(15, 15, 15, 15));

        // 作者信息
        JLabel authorLabel = new JLabel("<html><b>作者信息</b><br>QQ: 197667332</html>");
        authorLabel.setFont(new Font("微软雅黑", Font.PLAIN, 12));

        // 声明文本
        JTextArea declarationArea = new JTextArea();
        declarationArea.setText("免责声明：\n本软件完全免费，所有数据来源于公开网络，仅用于个人学习交流。严禁用于商业用途，若产生任何法律纠纷与作者无关。\n网盘密码:6gdr");
        declarationArea.setEditable(false);
        // 设置declarationArea的自动换行功能为true，使其在文本达到边界时自动换行
        declarationArea.setLineWrap(true);
        // 设置declarationArea的单词换行功能为true，使其在单词达到边界时自动换行，而不是在单词中间换行
        declarationArea.setWrapStyleWord(true);
        // 设置declarationArea的背景颜色为frame的背景颜色
        // 这行代码的作用是将declarationArea组件的背景颜色设置为与frame组件相同的背景颜色
        // declarationArea: 一个组件，可能是JPanel、JTextArea等
        // setBackground: 设置组件的背景颜色
        // frame.getBackground(): 获取frame组件的背景颜色
        declarationArea.setBackground(frame.getBackground());

        // 更新按钮
        JButton updateButton = new JButton("检查更新");
        updateButton.addActionListener(this::handleUpdate);

        // 组装面板
        mainPanel.add(authorLabel, BorderLayout.NORTH);
        mainPanel.add(new JScrollPane(declarationArea), BorderLayout.CENTER);
        mainPanel.add(updateButton, BorderLayout.SOUTH);

        frame.add(mainPanel, BorderLayout.CENTER);

        frame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosed(WindowEvent e) {
                frame = null;
            }
        });

        frame.setVisible(true);
    }

    private void handleUpdate(ActionEvent e) {
        try {
            if (Desktop.isDesktopSupported()) {
                Desktop desktop = Desktop.getDesktop();
                if (desktop.isSupported(Desktop.Action.BROWSE)) {
                    desktop.browse(new URI(UPDATE_URL));
                }
            }
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(frame,
                    "无法打开浏览器: " + ex.getMessage(),
                    "错误",
                    JOptionPane.ERROR_MESSAGE);
        }
    }
}